{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "# file = \"./app/src/main/res/layout/activity_sbscrb_short.xml\"\n",
    "# file = \"./app/src/main/res/layout/activity_invite.xml\"\n",
    "# file = \"C:\\\\Users\\\\linky\\\\Documents\\\\work\\\\xf\\\\xfcar\\\\app\\\\src\\\\main\\\\res\\\\layout\\\\fragment_mine.xml\"\n",
    "# file = \"C:\\\\Users\\\\linky\\\\Documents\\\\work\\\\xf\\\\xfcar\\\\app\\\\src\\\\main\\\\res\\\\layout\\\\activity_buy_or_rent_car.xml\"\n",
    "# file_dir = \"C:\\\\Users\\\\linky\\\\Documents\\\\work\\\\xf\\\\xfcar\\\\app\\\\src\\\\main\\\\res\\\\layout\\\\\"\n",
    "file_dir = \"/home/linky/Documents/myproject/xfcar/app/src/main/res/layout/\"\n",
    "# file_name = \"activity_modify_pwd.xml\"\n",
    "file_name = \"item_buy_rent_plan.xml\"\n",
    "\n",
    "file = file_dir + file_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(file) as f:\n",
    "    content = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile(\"android:id=\\\"@\\+id/(.*)\\\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "private TextView mTvItemName;\n",
      "private TextView mTvItemValue;\n",
      "private TextView mTvItemName2;\n",
      "private TextView mTvItemValue2;\n",
      "private TextView mTvItemName3;\n",
      "private TextView mTvItemValue3;\n"
     ]
    }
   ],
   "source": [
    "for vid in re.findall(pattern, content):\n",
    "    if \"tv_\" in vid:\n",
    "        print(\"private TextView mTv{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))\n",
    "    elif \"iv_\" in vid:\n",
    "        print(\"private ImageView mIv{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))\n",
    "    elif \"et_\" in vid:\n",
    "        print(\"private EditText mEt{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))\n",
    "    elif \"btn_\" in vid:\n",
    "        print(\"private Button mBtn{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))\n",
    "    elif \"ll_\" in vid:\n",
    "        print(\"private View mV{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))\n",
    "    elif \"sp_\" in vid:\n",
    "        print(\"private Spinner mSp{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))\n",
    "    elif \"rv_\" in vid:\n",
    "        print(\"private RecyclerView mRv{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))\n",
    "    elif \"v_\" in vid:\n",
    "        print(\"private View mV{};\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mTvItemName = findViewById(R.id.tv_item_name);\n",
      "mTvItemValue = findViewById(R.id.tv_item_value);\n",
      "mTvItemName2 = findViewById(R.id.tv_item_name_2);\n",
      "mTvItemValue2 = findViewById(R.id.tv_item_value_2);\n",
      "mTvItemName3 = findViewById(R.id.tv_item_name_3);\n",
      "mTvItemValue3 = findViewById(R.id.tv_item_value_3);\n"
     ]
    }
   ],
   "source": [
    "for vid in re.findall(pattern, content):\n",
    "    if \"tv_\" in vid:\n",
    "        print(\"mTv{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))\n",
    "    elif \"iv_\" in vid:\n",
    "        print(\"mIv{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))\n",
    "    elif \"et_\" in vid:\n",
    "        print(\"mEt{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))\n",
    "    elif \"btn_\" in vid:\n",
    "        print(\"mBtn{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))\n",
    "    elif \"ll_\" in vid:\n",
    "        print(\"mV{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))\n",
    "    elif \"sp_\" in vid:\n",
    "        print(\"mSp{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))\n",
    "    elif \"rv_\" in vid:\n",
    "        print(\"mRv{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))\n",
    "    elif \"v_\" in vid:\n",
    "        print(\"mV{} = findViewById(R.id.{});\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]]), vid))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mTvType.setText(mClaimPayBean.);\n",
      "mTvAmount.setText(mClaimPayBean.);\n",
      "mTvReceipt.setText(mClaimPayBean.);\n",
      "mTvDate.setText(mClaimPayBean.);\n"
     ]
    }
   ],
   "source": [
    "for vid in re.findall(pattern, content):\n",
    "    print(\"mTv{}.setText(mClaimPayBean.);\".format(\"\".join([c.capitalize() for c in vid.split(\"_\")[1:]])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "findViewById(R.id.rl_return_back).setOnClickListener(this);\n",
      "findViewById(R.id.tv_title).setOnClickListener(this);\n",
      "findViewById(R.id.sp_rent_car).setOnClickListener(this);\n",
      "findViewById(R.id.tv_buy_plan).setOnClickListener(this);\n",
      "findViewById(R.id.v_buy_plan).setOnClickListener(this);\n",
      "findViewById(R.id.tv_car_intro).setOnClickListener(this);\n",
      "findViewById(R.id.v_car_intro).setOnClickListener(this);\n",
      "findViewById(R.id.fl_container).setOnClickListener(this);\n"
     ]
    }
   ],
   "source": [
    "for vid in re.findall(pattern, content):\n",
    "    print(\"findViewById(R.id.{}).setOnClickListener(this);\".format(vid))    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "} else if (id == R.id.tv_price) {\n",
      "} else if (id == R.id.tv_buy) {\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "for vid in re.findall(pattern, content):\n",
    "    if \"et\" in vid or \"iv\" in vid:\n",
    "        continue\n",
    "        \n",
    "    print(\"}} else if (id == R.id.{}) {{\".format(vid))\n",
    "print(\"}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
